<?php

/**
 * Created by PhpStorm.
 * User: shwy
 * Date: 2017/5/17
 * Time: 13:37
 * 公共控制器
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        $this->load->model('role_model');
        $this->load->model('permission_model');
		
		$username = $this->session->userdata('username');
		$userid   = $this->session->userdata('userid');
		
		if (!$username || !$userid) {
			redirect('admin/login/index');
		}

        //访问权限控制
        $a = '技术,管理员';
        // 改为根据用户id查询roleId
        $arg = [
            'id' => $userid
        ];
        $userResult = $this->user_model->getByFieldQueryData('id,role_id', $arg, 0); //根据用户id,查询权限id

        $roleId = explode(',', $userResult['role_id']);

//        $subUrl = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '?'));//获取当前的url截取后的字符串

        //供应商是否有别的角色
        // if($roleId[0] == '供应商'){
        //     if($_SERVER['REQUEST_URI'] != '/admin/bidding/waitingList?page=1'){
        //         redirect('/admin/bidding/waitingList?page=1');
        //     }
        // }

        foreach ($roleId as $k => $v) {
            //在根据roleId读取 == name   role表,  读取permission_id
            $arg = [
                'name' => $v
            ];
            $permissionId[] = $this->role_model->getByFieldQueryData('*', $arg, 0);
        }

        $ids = array_column($permissionId, 'permission_id');

        //在根据permission_id == id 读取 permission
        foreach ($ids as $k2 => $v2) {
            $idsData[] = explode(',', $v2);

//            $emptyIds = array_unique($ids);  //去除相同的id
        }

        foreach ($idsData as $k3 => $v3) {
            foreach ($v3 as $k4 => $v4) {
                $arg2 = [
                    'id' => $v4
                ];
				
                $permissionIdData[] = $this->permission_model->getByFieldQueryData('*', $arg2, 0);
            }


        }
        // /admin/check/index?page=1&type=1
        if (strrpos($_SERVER['REQUEST_URI'], '?')) {
            $subUrl = substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'], '?')); //获取当前的url截取后的字符串

        } else {
            $subUrl = $_SERVER['REQUEST_URI']; //获取当前的url截取后的字符串
        }

//      $subUrl = '/admin/procurement/barterOrderAddView';
        foreach ($permissionIdData as $k4 => $v4) {
            if ($permissionIdData[$k4]) {
                //在用permission 跟 $_SERVER['REQUEST_URI']  比较,如果相等, 就表示有权限,如果没有就没有
                $explodeData[] = explode(',', $v4['permission']);  //根据逗号分割字符串成数组

            }
        }

        foreach ($explodeData as $k5 => $v5) {
            if (in_array($subUrl, $explodeData[$k5])) {
                $arr[] = '存在';
            } else {
                $arr[] = '不存在';
            }
        }

        if(!in_array('存在',$arr)){
            die( __alert('没有权限!'));
        }
    }

    
    public function get()
    {
        $this->load->view('index');
    }


}


